﻿/* 功能:  产品分类
 * 创建人：Kencery  创建时间：2018-2-1
 */
var ml = window.ml || {};
ml.admin = ml.admin || {};

ml.admin.productcategory = (function () {
    var defaults = {
        listUrl: "/ProductCategory/List",
        addUrl: "/ProductCategory/Add",
        editUrl: "/ProductCategory/Edit",
        delUrl: "/ProductCategory/Delete",
        MerchantId: "",
    };
    var opt = {};

    var columns = [{
        field: 'state',
        radio: true
    }, {
        title: '行号',
        field: 'number',
        align: 'center',
        hide: true,
        formatter: plscommon.tableNumber
    }, {
        title: '名称',
        field: 'Name'
    }, {
        title: '排序',
        field: 'Sort',
        align: 'center',
    }, {
        title: '是否启用',
        field: 'Enable',
        align: 'center',
        formatter: function (value, rows, index) {
            var result = returnEnableType(value);
            return '<div id="Enable_' + rows.Id + '">' + result + '</div>';
        }
    }, {
        title: '创建时间',
        field: 'CreateTime',
        align: 'center',
    }];

    var initTable = function () {
        plscommon.bootstraptable({
            id: "#productcategoryinfo",
            url: defaults.listUrl,
            queryParams: queryParams,
            uniqueId: "Id",
            columns: columns
        });
    };

    var queryParams = function (params) {
        return {
            offset: params.offset,                  //后台计算显示数据信息
            pagesize: params.limit,                 //每页显示多少行
            Name_Search: $("#name_search").val()
        };
    };

    var clickEvent = function () {
        $("#btnQueryList").on("click", function () { btnQueryList(); });                                     //按条件查询结果
        $("#btnReset").on("click", function () { btnReset(); });                                             //清空文本框信息

        $("#btnAddProductCategoryDialog").on("click", function () { btnAddProductCategoryDialog() });         //添加弹出浮层
        $("#btnEditProductCategoryDialog").on("click", function () { btnEditProductCategoryDialog() });       //修改弹出浮层
        $("#btnAddProductCategoryOK").on("click", function () { btnAddProductCategoryOK(); });                //添加修改方法

        $("#btnDelProductCategoryDialog").on("click", function () { btnDelProductCategoryDialog(); });        //删除弹出浮层
        $("#btnDeleteProductCategoryOK").on("click", function () { btnDeleteProductCategoryOK(); });          //删除方法 
    };

    var btnQueryList = function () {
        plscommon.refreshTable("productcategoryinfo");
    }

    var btnReset = function () {
        plscommon.resetFrom("formSearch");
    }

    var btnAddProductCategoryDialog = function () {
        if (defaults.MerchantId == "") {
            plscommon.warningMessage("该商户还未配置店铺信息，不允许添加", 4000);
            return false;
        }

        $('#AddProductCategoryModal').modal();
        plscommon.resetFrom("AddProductCategoryOperation");
        $("#HeadTitle").text("添加菜品分类");
        opt.type = 1;
    }

    var btnEditProductCategoryDialog = function () {
        //必须选中一条才能弹出
        var data = plscommon.dialogWaring('productcategoryinfo', 'AddProductCategoryModal', 'AddProductCategoryOperation');
        if (!data.Id) {
            return false;
        }

        $('#AddProductCategoryModal').modal();
        plscommon.resetFrom("AddProductCategoryOperation");
        $("#HeadTitle").text("修改菜品分类");
        opt.type = 2;
        //赋值给控件信息
        $("#Id").val(data.Id);
        $("#Name").val(data.Name);
        $("#Sort").val(data.Sort);
        $("#Enable").val(data.Enable);
    }

    var btnAddProductCategoryOK = function () {
        //读取form表单中的值并且进行验证
        var data = plscommon.deleteByValue($("#AddProductCategoryOperation").serializeArray(), "Id")
        data = plscommon.transitionArray(data);

        if (!checkData(data)) {
            return false;
        }

        $("#btnAddProductCategoryOK").prop("disabled", true);
        if (opt.type == 1) {
            plscommon.ajax_status({
                disableId: "btnAddProductCategoryOK",
                url: defaults.addUrl,
                type: "POST",
                data: data,
                success: function () {
                    if (this.status_code === 200) {
                        $('#AddProductCategoryModal').modal('hide');
                        plscommon.refreshTable("productcategoryinfo");
                    } else {
                        plscommon.warningMessage(this.status_message, 3000);
                    }
                }
            });
        } else {
            data.id = $("#Id").val();
            plscommon.ajax_status({
                disableId: "btnAddProductCategoryOK",
                url: defaults.editUrl,
                type: "POST",
                data: data,
                success: function () {
                    if (this.status_code === 200) {
                        $('#AddProductCategoryModal').modal('hide');
                        plscommon.refreshTable("productcategoryinfo");
                    } else {
                        plscommon.warningMessage(this.status_message, 3000);
                    }
                }
            });
        }
    }


    var btnDelProductCategoryDialog = function () {
        var data = plscommon.dialogWaring('productcategoryinfo', 'DelProductCategoryModal', 'DelProductCategoryOperation');
        if (!data.Id) {
            return false;
        }

        $("#delete_id").val(data.Id);
        $("#Delete_Head_Title").text(data.Name);
        $("#Delete_Head_Title_OK").text(data.Name);
    }

    var btnDeleteProductCategoryOK = function () {
        var delete_id = $("#delete_id").val();
        if (!delete_id) {
            plscommon.warningMessage("请您选择需要删除的数据", 4000);
            return false;
        }

        $("#btnDeleteProductCategoryOK").prop("disabled", true);
        plscommon.ajax({
            disableId: "btnDeleteProductCategoryOK",
            url: defaults.delUrl,
            type: "POST",
            data: { id: delete_id },
            success: function () {
                $('#DelProductCategoryModal').modal('hide');
                $("#productcategoryinfo").bootstrapTable('refresh');
            }
        });
    };

    var checkData = function (data) {
        if (!data.Name || !data.Sort) {
            plscommon.warningMessage("菜品分类名称和排序不能为空，请您检查", 3000);
            return false;
        }
        var reg = new RegExp("^[0-9]*$");
        if (!reg.test(data.Sort)) {
            plscommon.warningMessage("菜品类别排序只能是数字，请您检查", 3000);
            return false;
        }
        return true;
    }

    var returnEnableType = function (value) {
        var result = "";
        if (value == 1) {
            result = "禁用";
        }
        if (value == 0) {
            result = "启用";
        }
        return result;
    }

    return {
        init: function (options) {
            $.extend(defaults, options || {});
            initTable();            //初始化Table表格
            clickEvent();           //触发事件
        }
    };
}());